﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Version Check
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["version"] = new Object();
myPlugins["version"]["name"] = getLocalizedString2("mod_sysm_plugin_version_name");
myPlugins["version"]["onlyonce"] = true;
myPlugins["version"]["newObject"] = function (NewSettings) { return new plugin_version_sensor(NewSettings); }

var cachehit = 0;

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_version_sensor(NewSettings) {
	// Required variables
	this.plugin = "version";
	this.settings = NewSettings;
	this.edit = plugin_version_edit;
	this.ReDraw = plugin_version_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	//this.frame.style.backgroundColor = "red";


	// Warning about Anonymouse Statistics
	if (preferences.mod_sysmonitor_anonstat.value=="0") {
		anon = alert(getLocalizedString2("mod_sysm_anon_askagain"),getLocalizedString2("mod_sysm_anon_continue"),getLocalizedString2("mod_sysm_anon_cancel"));
		if(anon==1) {
			preferences.mod_sysmonitor_anonstat.value = 1;
			savePreferences();
		}
		if(anon==2) {
			this.canceledplugin = true;
			return false;
		}
	}


	// Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.updspeed) this.settings.updspeed = 21600;
	if(!this.settings.beta) this.settings.beta = false;
	if(!this.settings.showtime) this.settings.showtime = false;


	// The Sensor
	this.frame.onMultiClick = function ( ) { openURL("http://www.bluepaw.de/projects/widgets/SysWatch.php"); };
	this.frame.tooltip = getLocalizedString2("mod_sysm_plugin_version_tooltip");
	this.version = new class_TextLine(this.frame,0,"");
	this.version.label.color = this.settings.color;
	this.version.data.color = this.settings.color;
	this.version.data.alignment = "center";
	this.version.data.hOffset = mod_sysmonitor_width / 2;
	this.version.data.width = mod_sysmonitor_width;
	this.version.data.scrolling = "off";
	this.version.data.data = getLocalizedString2("mod_sysm_plugin_version_searchversion");
	this.KnownVersions = new Array();


	// Update Timer
	var obj = this;
	this.url = new URL();

	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_version_getit.call(obj); };

	this.timerClock = new Timer();
	this.timerClock.interval = 60;
	this.timerClock.ticking = 0;
	this.timerClock.onTimerFired = function ( ) { plugin_version_ShowClock.call(obj); };

	plugin_version_getit.call(obj);

	// Calculating sensore height
	this.frame.height = this.version.data.vOffset + (this.version.data.size * 1/3);
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_version_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(21600,43200);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_version_pref_beta");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.beta;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_version_pref_showtime");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.showtime;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.version.label.color = this.settings.color;
			this.version.data.color = this.settings.color;

			if ( this.settings.updspeed != Drive_Edit_DialogResults[1] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[1];
				this.timer.interval = this.settings.updspeed;
			}

			if ( this.settings.beta != Drive_Edit_DialogResults[2] ) {
				this.settings.beta = Drive_Edit_DialogResults[2];
			}

			this.settings.showtime = Drive_Edit_DialogResults[3];

			plugin_version_getit.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_version_getit() {
		this.timerClock.ticking = 0;
		this.version.data.scrolling = "off";
		this.version.data.data = getLocalizedString2("mod_sysm_plugin_version_searchversion");
		if(preferences.mod_sysmonitor_guid.value=="") {
			mod_sysmonitor_GetGUID();
			this.timer.interval = 1;
		} else {
			this.timer.interval = this.settings.updspeed;
			this.url.location = "http://tools.bluepaw.de/widgets/version_syswatch.php?guid=" + preferences.mod_sysmonitor_guid.value;
			this.url.fetchAsync( function() { plugin_version_getit_done.call(obj) } );
		}
	}

	function plugin_version_getit_done() {
		if(mod_sysmonitor_debug>2) print("plugin_version_getit_done: " + this.url.response);
		if(this.url.response==200) {
			this.KnownVersions = this.url.result.split(":");
			
			if(widget.version < parseFloat(this.KnownVersions[((this.settings.beta==true)?1:0)])) {
				this.timerClock.ticking = 0;
				this.version.data.data = getLocalizedString2("mod_sysm_plugin_version_newavailable") + this.KnownVersions[((this.settings.beta==true)?1:0)];
				this.version.data.scrolling = "left";
			} else {
				this.version.data.data = getLocalizedString2("mod_sysm_plugin_version_nonew");
				if(this.settings.showtime == 1) this.timerClock.ticking = 1;
			}
		}
	}


	function plugin_version_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.version.size = mod_sysmonitor_size;
		this.version.data.hOffset = mod_sysmonitor_width / 2;
		this.version.data.width = mod_sysmonitor_width;
		this.frame.height = this.version.data.vOffset + (this.version.data.size * 1/3);
		return true;
	}

	function plugin_version_ShowClock() {
		if (this.timerClock.interval == 60) this.timerClock.interval = 1;
		var date = new Date();
		this.version.data.data = date.toLocaleString();
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END